{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# January 21, 2016 In-class activities\n", "\n", "## Problem 1\n", "\n", "Write a function countdown() that takes a number and prints:\n", "\n", "```\n", "N\n", "N-1\n", "N-2\n", "...\n", "1\n", "Liftoff!\n", "```\n", "\n", "where N is the number you give it.\n", "\n", "Example:\n", "\n", "```\n", "countdown(3);\n", "3\n", "2\n", "1\n", "Liftoff!\n", "```\n", "\n", "One possible answer:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added method countdown(int)\n", "\n" ] } ], "source": [ "void countdown(int n) {\n", " for (int i = n; i > 0; i--) {\n", " printf(\"%s\\n\", i);\n", " }\n", " printf(\"Liftoff!\");\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "9\n", "8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n", "1\n", "Liftoff!\n" ] } ], "source": [ "countdown(10)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "1\n", "Liftoff!\n" ] } ], "source": [ "countdown(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2\n", "\n", "Write a function `volume()` that takes an height, width, and length dimensions and returns the volume of a cube with those dimensions.\n", "\n", "One possible answer:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added method volume(double,double,double)\n", "\n" ] } ], "source": [ "double volume(double width, double height, double length) {\n", " return (width * height * length);\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 72.0\n", "| assigned to temporary variable $5 of type double\n", "\n" ] }, { "data": { "text/plain": [ "72.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "volume(3, 4, 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3\n", "\n", "Define a class to hold students. The constructor should take a name and a year of graduation. Add a method \"status\" that when called, says: \"You are a junior\" (or whatever). Assume that the year is 2016.\n", "\n", "``\n", "Student student1 = new Student(\"Milley Cyrus\", 2018);\n", "Student student2 = new Student(\"Taylor Swift\", 2012);\n", "``\n", "\n", "The status is one of:\n", "\n", "* not in college yet\n", "* firstyear\n", "* sophomore\n", "* junior\n", "* senior\n", "* graduated\n", "\n", "The beginning of a solution:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Student\n", "\n" ] } ], "source": [ "class Student {\n", " String name;\n", " int year;\n", " Student(String name, int year) {\n", " this.name = name;\n", " this.year = year;\n", " }\n", " void status() {\n", " printf(\"You are crazy!\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable miley of type Student with initial value Student@50675690\n", "\n" ] } ], "source": [ "Student miley = new Student(\"Miley Cyrus\", 2015);" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You are crazy!\n" ] } ], "source": [ "miley.status()" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }